package com.xxx.video.dynamic.activitys;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xxx.video.R;
import com.xxx.video.commen.activitys.BaseActivity;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.utils.ClientInfoUtils;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.utils.StatusBarUtil;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.commen.utils.ToastUtils;
import com.xxx.video.commen.views.FastIndexBar;
import com.xxx.video.dynamic.adapters.ContactListAdapter;
import com.xxx.video.dynamic.beans.AddContactsFriendBean;
import com.xxx.video.dynamic.beans.ContactBean;
import com.xxx.video.dynamic.presenters.AddContactsFriendsPresenter;
import com.xxx.video.dynamic.presenters.IAddContactsFriendsView;

import java.util.Collections;
import java.util.List;

import butterknife.BindView;

public class AddContactsFriendsActivity extends BaseActivity<AddContactsFriendsPresenter>
        implements IAddContactsFriendsView, BaseRecycleViewAdapter.OnItemClickListener<AddContactsFriendBean.ListBean> {
    @BindView(R.id.rcv_add_contact_friend_list)
    RecyclerView rcvAddContactFriendList;
    @BindView(R.id.fib_add_contact_friend_list)
    FastIndexBar fibAddContactFriendList;
    /**
     * request read contacts permission
     */
    String[] permissions = {"android.permission.READ_CONTACTS"};
    /**
     * request read contact permission
     */
    public static final int REQUEST_READ_CONTACT_PERMISSION_CODE = 300;
    private ContactListAdapter mContactListAdapter;


    @Override
    public void initToolbarData() {
        super.initToolbarData();
        showActionBar();
        StatusBarUtil.getInstance().setStatusBarTransparent(this, true);
        setIsShowActionbarLine(true);
        setActionBarTitle(R.string.tv_dynamic_fragment_contacts_add_friends);
    }

    @Override
    protected AddContactsFriendsPresenter createPresenter() {
        return new AddContactsFriendsPresenter(this, this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_add_contact_friends;
    }

    @Override
    public void initData(Bundle extras) {
        requestPermissions();
    }

    @Override
    public void showRootLayoutStatus(int status) {
        showLayoutStatus(status);
    }

    @Override
    public void requestPermissions() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
            /*
             * have permission
             */
            getPresenter().getContactFriendList();
        } else {
            /*
             * no permission
             */
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                // You can directly ask for the permission.
                requestPermissions(permissions, REQUEST_READ_CONTACT_PERMISSION_CODE);
            } else {
                ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_please_open_the_read_contact_permission));
            }
        }
    }

    @Override
    public void initContactListData2View(List<ContactBean> contactBeanList) {

    }

    @Override
    public void initData2View(AddContactsFriendBean successOb) {
        if (ListUtils.isEmpty(successOb.list)) {
            showLayoutStatus(2);
            return;
        }

        for (AddContactsFriendBean.ListBean listBean : successOb.list) {
            try {
                String chars = listBean.username.toLowerCase();
                char[] chars2 = chars.toCharArray();
                listBean.wordIndex = String.valueOf(chars2[0]);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Collections.sort(successOb.list, (lhs, rhs) -> lhs.wordIndex.compareToIgnoreCase(rhs.wordIndex));

        showLayoutStatus(1);
        rcvAddContactFriendList.setLayoutManager(new LinearLayoutManager(this));
        mContactListAdapter = new ContactListAdapter(this);
        rcvAddContactFriendList.setAdapter(mContactListAdapter);
        mContactListAdapter.setOnItemClickListener(this);
        mContactListAdapter.setData(successOb.list);
        fibAddContactFriendList.setOnLetterChangeListener(letter -> rcvAddContactFriendList.scrollToPosition(mContactListAdapter.scrollByLetter(letter)));
    }

    @Override
    public void addFriendSuccess(int position) {
        if (mContactListAdapter != null) {
            mContactListAdapter.addFriendSuccess(position);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST_READ_CONTACT_PERMISSION_CODE:
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Permission is granted. Continue the action or workflow
                    // in your app.
                    getPresenter().getContactFriendList();
                    ClientInfoUtils.getInstance().updateClientInfo(this);
                } else {
                    // Explain to the user that the feature is unavailable because
                    // the features requires a permission that the user has denied.
                    // At the same time, respect the user's decision. Don't link to
                    // system settings in an effort to convince the user to change
                    // their decision.
                    showLayoutStatus(3);
                    ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_please_open_the_read_contact_permission));
                }
                return;
            default:
                break;
        }
    }

    @Override
    public void onItemClick(AddContactsFriendBean.ListBean listBean, int position, int type) {
        getPresenter().addFriends(listBean, position);
    }
}
